package com.wanxi.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.wanxi.model.Company;
import com.wanxi.model.Manager;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class LoginFilter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String remoteAddr = request.getRemoteAddr();
        //登录检查逻辑
        String loginManager = RedisTool.getToRedis("loginManager"+remoteAddr);
        Manager o = JSON.parseObject(loginManager, new TypeReference<Manager>() {
        });

        if(o != null){
            //放行
            return true;
        }
        try {
            request.getRequestDispatcher("/noLogin").forward(request,response);
        } catch (IOException | ServletException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
}
